package com.zulucap.riftcalc;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.XmlResourceParser;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.ShapeDrawable;
import android.os.Bundle;
import android.text.Html;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.zulucap.R;

public class SoulTreeActivity extends BaseActivity{
	private static final String TAG = "MySoulTreeActivity";
	private static final int DIAG_DESCRIPTION = 0;
	
	Integer mPointsSpent = 0;
	Integer mTotalPointsSpent = 0;
	String mSoulString = "";
	String soul = "";
	Integer mSoulButton = 0;
	Boolean newSoul = true;
	Integer mRemPoints = 66;
	String mSouls[] = new String[3];
	int mSoulPointsSpent[] = new int[3];
	String trait[] = new String[5];
	ArrayList <String[]>traits = new ArrayList<String[]>();
	ArrayList <ShapeDrawable>shapes = new ArrayList<ShapeDrawable>();
	Bitmap bitmap;
	Canvas canvas;
	
	String dialog_name = "";
	String dialog_curText = "";
	String dialog_nextText = "";
	String dialog_rank = "";
	String dialog_ranks = "";
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.soul_tree);
        
        soul = getIntent().getExtras().getString("soul");
        mSoulString = getIntent().getExtras().getString("soulString");
        if(mSoulString==null) mSoulString = "";
        mSoulButton = getIntent().getExtras().getInt("button");
        newSoul = getIntent().getExtras().getBoolean("newSoul");
        mPointsSpent = getIntent().getExtras().getInt("pointsSpent");
        mTotalPointsSpent = getIntent().getExtras().getInt("totalPointsSpent");
        mSoulPointsSpent = getIntent().getExtras().getIntArray("soulPointsSpent");
        mSouls = getIntent().getExtras().getStringArray("souls");
        
        TextView txtCalling = (TextView) findViewById(R.id.textCalling);
        TextView txtPointsSpent = (TextView) findViewById(R.id.textSpentPoints);
        TextView textRemPoints = (TextView) findViewById(R.id.textRemPoints);
        
        String strSpentPoints = "";
        for(Integer j = 0; j< mSoulPointsSpent.length; j++){
        	strSpentPoints = strSpentPoints + "/" + mSoulPointsSpent[j];
        }
        strSpentPoints = strSpentPoints.substring(1);
        
        mRemPoints = mRemPoints - mTotalPointsSpent;
        txtCalling.setText(soul + " (" + mPointsSpent + ")");
        txtPointsSpent.setText(strSpentPoints);
        textRemPoints.setText("Points Left " + mRemPoints.toString());
        
        // Process souls
        XmlResourceParser xml_soul = getResources().getXml(getResources().getIdentifier(soul.toLowerCase().replaceAll(" ", ""),"xml","com.zulucap"));
       
        try {
        	InitializeTraits();
        	ParseSoul(soul, xml_soul);
        	ParseReqs();
        	if(!newSoul) loadTraits();
        	
        	/*
        	View treeTable = findViewById(R.id.treeTable);
        	ViewTreeObserver vto = treeTable.getViewTreeObserver();
        	vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
        		@Override
        		public void onGlobalLayout(){
        			View tableView =  findViewById(R.id.treeTable);
        			int tableHeight = tableView.getHeight();
        			int tableWidth = tableView.getWidth();
        			
        			bitmap = Bitmap.createBitmap(tableWidth, tableHeight, Bitmap.Config.ARGB_8888);
                    canvas = new Canvas(bitmap);
                    
                	DrawShape(0, 0, tableHeight, tableWidth, 0x88FF0000);
        			
        			//tv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        		}
        	});
            //int tableHeight = treeTable.getHeight();
            //int tableWidth = treeTable.getWidth();
            //bitmap = Bitmap.createBitmap(tableWidth, tableHeight, Bitmap.Config.ARGB_8888);
            //canvas = new Canvas(bitmap);
            
        	//DrawShape(0, 0, 200, 400, 0xFFFFFFFF);
        	 */
        	
        } catch (Exception e) {
        	// Fail
        }
    }
    
    
    
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
    	    if (keyCode == KeyEvent.KEYCODE_BACK)
    	    {
    	    	// Return Selected Value
    	    	Intent resultIntent = new Intent();
    	    	resultIntent.putExtra("soulString", mSoulString);
    	    	resultIntent.putExtra("pointsSpent", mPointsSpent);
    	    	resultIntent.putExtra("totalPointsSpent", mTotalPointsSpent);
    	    	resultIntent.putExtra("button", mSoulButton);
    	    	resultIntent.putExtra("soul", soul);
    	    	setResult(Activity.RESULT_OK, resultIntent);
    	    	
    	    	this.finish();
    	    }
    	    return true;
    }

    public void InitializeTraits(){
    	int row = 0;
    	int col = 0;
    	
    	for(row=0; row<7; row++){
    		for(col=0; col<4; col++){
    			// Get Frame
    			FrameLayout frame = (FrameLayout) findViewById(getResources().getIdentifier(
    					"trait_" + row + "_" + col, "id", "com.zulucap"));
    			
    			frame.setTag(R.id.trait, "false");
    		}
    	}
    	
    }
    
    public void ParseSoul( String soul, XmlResourceParser souls) throws XmlPullParserException,
    IOException {
    	int eventType = -1;
    	String _calling = "";
    	String displayRow = "";
    	String displayColumn = "";
    	String icon="";
    	String ranks = "";
    	String key = "";
    	String preReq = "";
    	String name = "";
    	boolean isRank = false;
    	boolean isAbility = false;
    	int curRank = 0;
    	String abilityDesc = "";
    	String descriptions[] = {};

    	Integer bg = getResources().getIdentifier("tree_" + soul.toLowerCase(), "drawable", "com.zulucap");
    	if(bg == 0){
    		findViewById(R.id.layout_soul_tree).setBackgroundResource(R.drawable.tree_default);
    	} else {
    		findViewById(R.id.layout_soul_tree).setBackgroundResource(bg);
    	}
    	
    	while (eventType != XmlResourceParser.END_DOCUMENT) {
            if (eventType == XmlResourceParser.START_TAG) { 
            	// Get the name of the tag (eg souls or soul)
                String strName = souls.getName();
                
                // New Trait
                if (strName.equals("Trait")){
                	displayRow = "";
                	displayColumn = "";
                	icon = "";
                	ranks = "";
                	key = "";
                	preReq = "null";
                	name = "";
                	isRank = false;
                	isAbility = false;
                	descriptions = new String[] {};
                	curRank = 0;
                	abilityDesc = "";
                }		
               
                if(strName.equals("Key")){
                	key = souls.nextText();
                }
                if (strName.equals("Name")){
                	name = souls.nextText();
                }
                if(strName.equals("PrereqTraitKey")){
                	preReq = souls.nextText();              
                }
                if(strName.equals("Icon")){
                	icon = souls.nextText().replaceAll("-", "_").toLowerCase();	
                }
                if (strName.equals("DisplayRow")){
                	displayRow = souls.nextText();
                }
                if (strName.equals("DisplayColumn")){	
                	displayColumn = souls.nextText();
                }
                if (strName.equals("Ability")){
                	isAbility = true;
                }
                if (strName.equals("Ranks")){
                	ranks = souls.getAttributeValue(null, "count");
                	descriptions = new String[Integer.decode(ranks)];
                	isRank = true;
                }
                if (strName.equals("Rank") && isRank){
                	curRank++;
                }
                if (strName.equals("Description")){
                	if(isRank){
                		descriptions[curRank-1] = (souls.nextText());
                	} else if(isAbility){
                		abilityDesc = (souls.nextText());
                		isAbility = false;
                	}
                }
            }
            
            if (eventType == XmlResourceParser.END_TAG ){
            	String strName = souls.getName();
            	
            	// Finished reading trait
            	// Process data
            	if (strName.equals("Trait")){
            		
            		trait[0] = key;
            		trait[1] = displayRow;
            		trait[2] = displayColumn;
            		trait[3] = preReq;
            		trait[4] = ranks;
            		
            		traits.add(new String[]{key, displayRow, displayColumn, preReq, ranks});
            		
            		// Get Image
            		ImageView img = (ImageView) findViewById(getResources().getIdentifier(
            				"img_trait_" + displayRow + "_" + displayColumn , "id", "com.zulucap"));
            		
            		img.setImageResource(getResources().getIdentifier(
            				icon, "drawable", "com.zulucap"));
            		
            		// Get Frame
            		FrameLayout frame = (FrameLayout) findViewById(getResources().getIdentifier(
        					"trait_" + displayRow + "_" + displayColumn, "id", "com.zulucap"));
            		
            		if(ranks.equals("1") && !(abilityDesc.equals(""))){
            			descriptions = new String[] {abilityDesc};
            		}
            		frame.setVisibility(View.VISIBLE);
            		frame.setTag(R.id.name, name);
            		frame.setTag(R.id.row, displayRow);
            		frame.setTag(R.id.column, displayColumn);
            		frame.setTag(R.id.ranks, ranks);
            		frame.setTag(R.id.rank, "0");
            		frame.setTag(R.id.trait, "true");
            		frame.setTag(R.id.descriptions, descriptions);
            		frame.setOnClickListener(trait_onClick);
            		frame.setOnLongClickListener(trait_onLongClick);
            		// Get Text
            		TextView textRanks = (TextView) findViewById(getResources().getIdentifier(
            				"trait_rank_" + displayRow + "_" + displayColumn, "id", "com.zulucap"));
            		
            		textRanks.setText("0/" + ranks);
            		
            		if(displayRow.equals("0")){
            			frame.setForeground(getResources().getDrawable(R.drawable.trait_active));
            		}
            		
            	}
            }
            eventType = souls.next();
    	}
    }
    
    public void ParseReqs(){
    	int index = 0;
    	int size = traits.size();
    	
//    	ArrayList <String[]>sortedTraits = new ArrayList<String[]>(size);
//    	sortedTraits = traits;
    	
    	Iterator<String[]> i = traits.iterator();
    	
    	while(i.hasNext()){
    		String aTrait[] = (String[]) i.next();
    		
//    		sortedTraits.set(index, aTrait);
    	
    		
    		
    		if(!aTrait[3].equals("null")){
		//    	i = sortedTraits.iterator();
    			ArrayList <String[]>preReqTraits = new ArrayList<String[]>(size);
    			preReqTraits = traits;
    			
    			Iterator<String[]> j = preReqTraits.iterator();
    			
    			while(j.hasNext()){
		    		String preReqTrait[] = (String[]) j.next();
	    		
		    		if(preReqTrait[0].equals(aTrait[3])){
	    		
//		    			String preReqTrait[] = aTrait.get(preReq);
//		    			int preReq = Integer.decode(aTrait[3]);
		    			String ranks[] = new String[2];
		    			
		    			
		    			FrameLayout frame = (FrameLayout) findViewById(getResources().getIdentifier(
		    					"trait_" + aTrait[1] + "_" + aTrait[2], "id", "com.zulucap"));
		    			
		    			// Get Text
		        		TextView prRanks = (TextView) findViewById(getResources().getIdentifier(
		        				"trait_rank_" + preReqTrait[1] + "_" + preReqTrait[2], "id", "com.zulucap"));
		        		ranks = ((String) prRanks.getText()).split("/");
		        		Integer maxRow = (mPointsSpent - (mPointsSpent % 5))/5;
		        		
		        		
		        		if(ranks[0].equals(ranks[1])){
		        			
		        			if( Integer.decode(aTrait[1]) <= maxRow || isPVP()){
		        				frame.setForeground(getResources().getDrawable(R.drawable.trait_active));
		        			}
		        			
		        		}
		        		else{
		        			
		        			if( Integer.decode(aTrait[1]) <= maxRow){
		        				frame.setTag(R.id.trait, "false");
		        				frame.setForeground(getResources().getDrawable(R.drawable.trait_inactive));
		        			}
		        		}
		        		
		        		DrawSeparator(Integer.decode(preReqTrait[1]), Integer.decode(preReqTrait[2]), Integer.decode(aTrait[1]), Integer.decode(aTrait[2]));
		        		
		        		// Stop looping
		        		continue;
		    		}
	    		}
	    	}
    	}

    }
    
    private OnClickListener trait_onClick = new OnClickListener(){
    	public void onClick(View v){
    	
    		String row = (String) v.getTag(R.id.row);
    		String column = (String) v.getTag(R.id.column);
    		String ranks = (String) v.getTag(R.id.ranks);
    		String rank = (String) v.getTag(R.id.rank);
    		
    		if ( (mPointsSpent >= Integer.decode(row) * 5 || isPVP()) && Integer.decode(rank) < Integer.decode(ranks) && mTotalPointsSpent < 66 ){
    			TextView textRanks = (TextView) findViewById(getResources().getIdentifier(
        				"trait_rank_" + row + "_" + column, "id", "com.zulucap"));
    			
    			textRanks.setText(Integer.decode(rank) + 1 + "/" + ranks);
    			Integer int_rank = Integer.decode(rank);
    			int_rank++;
    			v.setTag(R.id.rank, int_rank.toString() );
    			
    			String curString = row+column+rank + "/";
    			String newString = row+column+(Integer.decode(rank)+1)+"/";
    				
    			if(mSoulString.contains(curString)){
    				mSoulString = mSoulString.replace(curString, newString);
    			} else {
    				mSoulString = mSoulString + newString;
    			}

    			mPointsSpent++;
    			mTotalPointsSpent++;
    			
    			TextView txtCalling = (TextView) findViewById(R.id.textCalling);
    	        TextView txtPointsSpent = (TextView) findViewById(R.id.textSpentPoints);
    	        TextView textRemPoints = (TextView) findViewById(R.id.textRemPoints);
    	        
    	        String strSpentPoints = "";
    	        for(Integer j = 0; j< mSoulPointsSpent.length; j++){
    	        	strSpentPoints = strSpentPoints + "/" + mSoulPointsSpent[j];
    	        }
    	        strSpentPoints = strSpentPoints.substring(1);
    	        
    	        mRemPoints--;
    	        
    	        txtCalling.setText(soul + "(" + mPointsSpent + ")"); 
    	        txtPointsSpent.setText(strSpentPoints);
    	        textRemPoints.setText("Points Left " + mRemPoints.toString());    	        
    			
    		}
    		
    		if(!isPVP()){
	    		Integer maxRow = (mPointsSpent - (mPointsSpent % 5))/5;
	    		if(maxRow < 7){
		    		FrameLayout col1 = (FrameLayout) findViewById(getResources().getIdentifier(
		    				"trait_" + maxRow.toString() + "_0", "id", "com.zulucap"));
		    		FrameLayout col2 = (FrameLayout) findViewById(getResources().getIdentifier(
		    				"trait_" + maxRow.toString() + "_1", "id", "com.zulucap"));
		    		FrameLayout col3 = (FrameLayout) findViewById(getResources().getIdentifier(
		    				"trait_" + maxRow.toString() + "_2", "id", "com.zulucap"));
		    		FrameLayout col4 = (FrameLayout) findViewById(getResources().getIdentifier(
		    				"trait_" + maxRow.toString() + "_3", "id", "com.zulucap"));
		    		
		    		col1.setForeground(getResources().getDrawable(R.drawable.trait_active));
		    		col2.setForeground(getResources().getDrawable(R.drawable.trait_active));
		    		col3.setForeground(getResources().getDrawable(R.drawable.trait_active));
		    		col4.setForeground(getResources().getDrawable(R.drawable.trait_active));
	    		}
    		}
    		
    		// This could be optimized better
    		ParseReqs();
    	}
    };
//
    
    private OnLongClickListener trait_onLongClick = new OnLongClickListener(){
    	public boolean onLongClick(View v){
    		dialog_name = (String) v.getTag(R.id.name);
    		dialog_ranks = (String) v.getTag(R.id.ranks);
    		dialog_rank = (String) v.getTag(R.id.rank);
    		String descriptions[] = (String[]) v.getTag(R.id.descriptions);
    		dialog_curText = "";
    		dialog_nextText = "";
    		    		
    		// Trait Full - No Next
    		if(dialog_rank.equals(dialog_ranks)){
    			dialog_curText = descriptions[Integer.decode(dialog_rank)-1];
    		} else if(dialog_rank.equals("0")){
    			// Trait Empty - No Current
    			//description = descriptions[Integer.decode(rank)-1];
    			dialog_nextText = descriptions[Integer.decode(dialog_rank)];
    		} else{
    			// Trait Partial - Current and Next
    			dialog_curText = descriptions[Integer.decode(dialog_rank)-1];
    			dialog_nextText = descriptions[Integer.decode(dialog_rank)];
    		}

    		showDialog(DIAG_DESCRIPTION);
    		
    		return true;
    	}
    };
    
    public void loadTraits(){
    	String traits[] = mSoulString.split("/");
    	Integer row = 0;
    	Integer col = 0;
    	Integer rank = 0;
    	
    	for(Integer i = 0; i < traits.length; i++){
    		row = Integer.decode(traits[i].substring(0, 1));
    		col = Integer.decode(traits[i].substring(1, 2));
    		rank = Integer.decode(traits[i].substring(2, 3));
    		
    		// Get Text
    		TextView textRanks = (TextView) findViewById(getResources().getIdentifier(
    				"trait_rank_" + row + "_" + col, "id", "com.zulucap"));
    		
    		textRanks.setText( textRanks.getText().toString().replace("0/", rank + "/") );
    		
    		// Get Frame
    		FrameLayout frame = (FrameLayout) findViewById(getResources().getIdentifier(
					"trait_" + row + "_" + col, "id", "com.zulucap"));
    		
    		frame.setTag(R.id.rank, rank.toString());
    	}
    	
    	Integer maxRow = (mPointsSpent - (mPointsSpent % 5))/5;
    	Integer rows = 0;
		while(rows < maxRow +1){
    		FrameLayout col1 = (FrameLayout) findViewById(getResources().getIdentifier(
    				"trait_" + rows.toString() + "_0", "id", "com.zulucap"));
    		FrameLayout col2 = (FrameLayout) findViewById(getResources().getIdentifier(
    				"trait_" + rows.toString() + "_1", "id", "com.zulucap"));
    		FrameLayout col3 = (FrameLayout) findViewById(getResources().getIdentifier(
    				"trait_" + rows.toString() + "_2", "id", "com.zulucap"));
    		FrameLayout col4 = (FrameLayout) findViewById(getResources().getIdentifier(
    				"trait_" + rows.toString() + "_3", "id", "com.zulucap"));
    		
    		if(col1.getTag(R.id.trait).equals("true")){
    			col1.setForeground(getResources().getDrawable(R.drawable.trait_active));
    		}
    		if(col2.getTag(R.id.trait).equals("true")){
    			col2.setForeground(getResources().getDrawable(R.drawable.trait_active));
    		}
    		if(col3.getTag(R.id.trait).equals("true")){
    			col3.setForeground(getResources().getDrawable(R.drawable.trait_active));
    		}
    		if(col4.getTag(R.id.trait).equals("true")){
    			col4.setForeground(getResources().getDrawable(R.drawable.trait_active));
    		}
    		
    		rows++;
		}
		
		if(isPVP()){
			FrameLayout col1 = (FrameLayout) findViewById(getResources().getIdentifier(
    				"trait_0_1", "id", "com.zulucap"));
    		FrameLayout col2 = (FrameLayout) findViewById(getResources().getIdentifier(
    				"trait_0_2", "id", "com.zulucap"));
    		
    		TextView ranks1 = (TextView) findViewById(getResources().getIdentifier(
    				"trait_rank_0_1", "id", "com.zulucap"));
    		TextView ranks2 = (TextView) findViewById(getResources().getIdentifier(
    				"trait_rank_0_2", "id", "com.zulucap"));
    		
    		if(ranks1.getText().equals("5/5")){
    			col1.performClick();
    		}
    		if(ranks2.getText().equals("5/5")){
    			col2.performClick();
    		}
		}
			
    }

    public boolean isPVP(){
    	if(soul.equals("Archmage") || 
    		soul.equals("Templar") ||
    		soul.equals("Infiltrator") ||
    		soul.equals("Vindicator")){
    		
    		return true;
    	} else {	
    		return false;
    	}
    }

    public void DrawSeparator(int row1, int col1, int row2, int col2){
    	
    	
    	if(row1 == row2){
    	// Same row
    		
    		if(col2 > col1){
    			if(col2 == col1+1){
	    			// Draw to right of col1
	    			ImageView separator = (ImageView) findViewById(getResources().getIdentifier("arrow_" + row1 + "_" + col1 + "x", "id", "com.zulucap"));
	    			separator.setBackgroundDrawable(getResources().getDrawable(R.drawable.x_connector));
    			} else {
    				ImageView separator = (ImageView) findViewById(getResources().getIdentifier("arrow_" + row1 + "_" + col1 + "x", "id", "com.zulucap"));
	    			separator.setBackgroundDrawable(getResources().getDrawable(R.drawable.x_connector));
	    			
    				int distance = col2-col1;
    				for(int i = 1; i<distance; i++){
    					
    					FrameLayout theFrame = (FrameLayout) findViewById(getResources().getIdentifier("trait_" + row1 + "_" + col1+i,"id","com.zulucap"));
    					theFrame.setVisibility(View.VISIBLE);
    					theFrame.setForeground(getResources().getDrawable(R.drawable.x_connector));
    					theFrame.setTag(R.id.trait, "false");

    					ImageView theImage = (ImageView) findViewById(getResources().getIdentifier("img_trait_" + row1 + "_" + col1+i,"id","com.zulucap"));
    					theImage.setImageDrawable(null);
    					
    					TextView theText = (TextView) findViewById(getResources().getIdentifier("trait_rank_" + row1 + "_" + col1+i,"id","com.zulucap"));
    					theText.setVisibility(View.GONE);
    					
    					ImageView nextSeparator = (ImageView) findViewById(getResources().getIdentifier("arrow_" + row1 + "_" + col1+i + "x", "id", "com.zulucap"));
    	    			nextSeparator.setBackgroundDrawable(getResources().getDrawable(R.drawable.x_connector));
    				}
    			
    			}
    		} else{
	    		if(col2 == col1-1){
	    			// Draw to left of col1
	    			ImageView separator = (ImageView) findViewById(getResources().getIdentifier("arrow_" + row1 + "_" + (col1-1) + "x", "id", "com.zulucap"));
	    			separator.setBackgroundDrawable(getResources().getDrawable(R.drawable.x_connector));
    			} else {
    				ImageView separator = (ImageView) findViewById(getResources().getIdentifier("arrow_" + row1 + "_" + col1 + "x", "id", "com.zulucap"));
	    			separator.setBackgroundDrawable(getResources().getDrawable(R.drawable.x_connector));
	    			
    				int distance = col2-col1;
    				for(int i = 1; i<distance; i++){
    					int j = col1-i;
    					FrameLayout theFrame = (FrameLayout) findViewById(getResources().getIdentifier("trait_" + row1 + "_" + j,"id","com.zulucap"));
    					theFrame.setVisibility(View.VISIBLE);
    					theFrame.setForeground(getResources().getDrawable(R.drawable.x_connector));
    					theFrame.setTag(R.id.trait, "false");
    					
    					ImageView theImage = (ImageView) findViewById(getResources().getIdentifier("img_trait_" + row1 + "_" + j,"id","com.zulucap"));
    					theImage.setImageDrawable(null);
    					
    					TextView theText = (TextView) findViewById(getResources().getIdentifier("trait_rank_" + row1 + "_" + j,"id","com.zulucap"));
    					theText.setVisibility(View.GONE);
    					
    					ImageView nextSeparator = (ImageView) findViewById(getResources().getIdentifier("arrow_" + row1 + "_" + j + "x", "id", "com.zulucap"));
    					nextSeparator.setBackgroundDrawable(getResources().getDrawable(R.drawable.x_connector));
    				}
    			}
    		}
    		
    	} else if(col1 == col2){
    	// Same col
    			
    		if(row2 > row1){
    			if(row2 == row1+1){
	    			//Draw above row1
	    			ImageView separator = (ImageView) findViewById(getResources().getIdentifier("arrow_" + row1 + "_" + col1 + "y", "id", "com.zulucap"));
	    			separator.setBackgroundDrawable(getResources().getDrawable(R.drawable.y_connector));
    			} else {
    				ImageView separator = (ImageView) findViewById(getResources().getIdentifier("arrow_" + row1 + "_" + col1 + "y", "id", "com.zulucap"));
	    			separator.setBackgroundDrawable(getResources().getDrawable(R.drawable.y_connector));
	    			
    				int distance = row2-row1;
    				for(int i = 1; i<distance; i++){
    					int j = row1 + i;
    					FrameLayout theFrame = (FrameLayout) findViewById(getResources().getIdentifier("trait_" + j + "_" + col1,"id","com.zulucap"));
    					theFrame.setVisibility(View.VISIBLE);
    					theFrame.setForeground(getResources().getDrawable(R.drawable.y_connector));
    					theFrame.setTag(R.id.trait, "false");
    					
    					ImageView theImage = (ImageView) findViewById(getResources().getIdentifier("img_trait_" + j + "_" + col1,"id","com.zulucap"));
    					theImage.setImageDrawable(null);
    					
    					TextView theText = (TextView) findViewById(getResources().getIdentifier("trait_rank_" + j + "_" + col1,"id","com.zulucap"));
    					theText.setVisibility(View.GONE);
    					
    					ImageView nextSeparator = (ImageView) findViewById(getResources().getIdentifier("arrow_" + j + "_" + col1 + "y", "id", "com.zulucap"));
    	    			nextSeparator.setBackgroundDrawable(getResources().getDrawable(R.drawable.y_connector));
    	    			
    				}
    			}
    		} else{
    			//Draw below row1

    			ImageView separator = (ImageView) findViewById(getResources().getIdentifier("arrow_" + (row1-1) + "_" + col1 + "x", "id", "com.zulucap"));
    			separator.setBackgroundDrawable(getResources().getDrawable(R.drawable.y_connector));
    		}
    		
    	} else{
    	// L Connector
    		
    		//     |
    		//   --|
    		// x+1, y+1
    		if(col2 > col1){
    			// Draw to right
    			ImageView separator = (ImageView) findViewById(getResources().getIdentifier("arrow_" + row1 + "_" + col1 + "x", "id", "com.zulucap"));
    			separator.setBackgroundDrawable(getResources().getDrawable(R.drawable.x_connector));
    			
    			//Fill Up
    			int j = col1+1;
    			FrameLayout theFrame = (FrameLayout) findViewById(getResources().getIdentifier("trait_" + row1 + "_" + j,"id","com.zulucap"));
				theFrame.setVisibility(View.VISIBLE);
				theFrame.setForeground(getResources().getDrawable(R.drawable.lr_yx_connector));
				theFrame.setTag(R.id.trait, "false");
				
				ImageView theImage = (ImageView) findViewById(getResources().getIdentifier("img_trait_" + row1 + "_" + j,"id","com.zulucap"));
				theImage.setImageDrawable(null);
    			
				TextView theText = (TextView) findViewById(getResources().getIdentifier("trait_rank_" + row1 + "_" + j,"id","com.zulucap"));
				theText.setVisibility(View.GONE);
				
    			ImageView nextSeparator = (ImageView) findViewById(getResources().getIdentifier("arrow_" + row1 + "_" + j + "y", "id", "com.zulucap"));
    			nextSeparator.setBackgroundDrawable(getResources().getDrawable(R.drawable.y_connector));
    			
    		} else {
    			// Draw to left
    			int j = col1-1;
    			ImageView separator = (ImageView) findViewById(getResources().getIdentifier("arrow_" + row1 + "_" + j + "x", "id", "com.zulucap"));
    			separator.setBackgroundDrawable(getResources().getDrawable(R.drawable.x_connector));
    			
    			//Fill Up
    			FrameLayout theFrame = (FrameLayout) findViewById(getResources().getIdentifier("trait_" + row1 + "_" + j,"id","com.zulucap"));
				theFrame.setForeground(null);
				theFrame.setVisibility(View.VISIBLE);
				theFrame.setTag(R.id.trait, "false");
				
				ImageView theImage = (ImageView) findViewById(getResources().getIdentifier("img_trait_" + row1 + "_" + j,"id","com.zulucap"));
				theImage.setPadding(0, 0, 0, 0);
				theImage.setImageDrawable(getResources().getDrawable(R.drawable.rl_xy_connector));
				//theImage.setVisibility(View.INVISIBLE);
    			
				TextView theText = (TextView) findViewById(getResources().getIdentifier("trait_rank_" + row1 + "_" + j,"id","com.zulucap"));
				//theText.setText("");
				//theText.setBackgroundColor(0x00000000);
				theText.setVisibility(View.GONE);

    			ImageView nextSeparator = (ImageView) findViewById(getResources().getIdentifier("arrow_" + row1 + "_" + j + "y", "id", "com.zulucap"));
    			nextSeparator.setBackgroundDrawable(getResources().getDrawable(R.drawable.y_connector));
    		}
    	}
    }
    
    @Override
    protected Dialog onCreateDialog(int id) {
         switch(id) {
         case DIAG_DESCRIPTION:
          LayoutInflater inflater = 
        	  (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          final View layout = 
        	  inflater.inflate(R.layout.trait_description, 
        	  		(ViewGroup) findViewById(R.id.layout_trait_description));
          
          //dialog = new Dialog(this);
          //dialog.setContentView(R.layout.trait_description);
          //dialog.setTitle(dialog_name);
          //dialog.setOwnerActivity(this);
          
          TextView currentText = (TextView) layout.findViewById(R.id.text_cur_description);
  	      ImageView separator = (ImageView) layout.findViewById(R.id.image_description_separator);
  		  TextView nextText = (TextView) layout.findViewById(R.id.text_next_description);
          
          // Trait Full - No Next
  		  if(dialog_rank.equals(dialog_ranks)){
  			
  			currentText.setText(Html.fromHtml(
  					"<strong>" + getResources().getString(R.string.curDescText) + "</strong><br />" + dialog_curText));
  			currentText.setVisibility(View.VISIBLE);
  		  } else if(dialog_rank.equals("0")){
  			// Trait Empty - No Current
 
  			nextText.setText(Html.fromHtml(
  					"<strong>" + getResources().getString(R.string.nextDescText) + "</strong><br />" + dialog_nextText));
  			nextText.setVisibility(View.VISIBLE);
  		  } else{
  			// Trait Partial - Current and Next
  			
  			currentText.setText(Html.fromHtml(
  					"<strong>" + getResources().getString(R.string.curDescText) + "</strong><br />" + dialog_curText));
  			currentText.setVisibility(View.VISIBLE);
  			
  			separator.setVisibility(View.VISIBLE);
  			
  			nextText.setText(Html.fromHtml(
  					"<strong>" + getResources().getString(R.string.nextDescText) + "</strong><br />" + dialog_nextText));
  			nextText.setVisibility(View.VISIBLE);
  		  }
  		  
  		  AlertDialog.Builder builder = new AlertDialog.Builder(this);
          builder.setView(layout);
          builder.setTitle(dialog_name);
          builder.setOnCancelListener(
        		  new Dialog.OnCancelListener(){
        			  public void onCancel(DialogInterface dialog){
        				  SoulTreeActivity.this.removeDialog(DIAG_DESCRIPTION);
        			  }
        		  }
          );
          
          AlertDialog descriptionDialog = builder.create();
          return descriptionDialog;
         }
         
		return null;
         
    }
}
